﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MyModules;

namespace WindowsFormsApplication2
{
    public partial class Example : Form
    {
        private ConfigManager example;

        public Example()
        {
            InitializeComponent();
            string path = Application.StartupPath + "//example.ini";
            this.example = new ConfigManager(new IniFile(path));


            //example.Add(checkBox1, this.GetType().ToString(), checkBox1.Name);
            //example.Add(comboBox1, this.GetType().ToString(), comboBox1.Name);
            //example.Add(numericUpDown1, this.GetType().ToString(), numericUpDown1.Name);


            //example.Add(checkBox1, "Section1", "Name1");
            //example.Add(comboBox1, "Section2", "Name2");
            //example.Add(numericUpDown1, "Section3", "Name3");


            foreach (Control item in this.Controls)
            {
                Type t = item.GetType();
                if(t == typeof(CheckBox) || t == typeof(ComboBox) || t == typeof(NumericUpDown))
                {
                    this.example.Add(item,this.GetType().ToString(),item.Name);
                }
            }
            this.example.AddReader(typeof(TextBox).ToString(), new ConfigManager.Reader(readTextBox));
            this.example.AddEventBind(typeof(TextBox).ToString(), new ConfigManager.EventBind(bindTextBox));
            this.example.Add(textBox1, this.GetType().ToString(), textBox1.Name);
            this.example.Init();
        }

        private string readTextBox(object item)
        {
            string error = null;
            string[] info = this.example.Bindings[item].Split(this.example.Seperator);
            string section = info[0];
            string key = info[1];
            string def = "";
            if (info.Length > 2)
            {
                def = info[2];
            }
            try
            {
                (item as TextBox).Text = this.example.Param.ReadString(section, key, def);
            }
            catch (Exception e)
            {
                error = e.Message.ToString();
            }
            return error;
        }

        private void bindTextBox(object item, bool remove = false)
        {
            if (remove)
            {
                (item as TextBox).TextChanged -= this.TextBoxTextChange;
            }
            else
            {
                (item as TextBox).TextChanged += this.TextBoxTextChange;
            }
        }

        private void TextBoxTextChange(object sender, EventArgs e)
        {
            string[] info = this.example.Bindings[sender].Split(this.example.Seperator);
            string section = info[0];
            string key = info[1];
            string value = (sender as TextBox).Text;
            this.example.Param.WriteString(section, key, value);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.example.RemoveEventBind(typeof(TextBox).ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.example.AddEventBind(typeof(TextBox).ToString(), new ConfigManager.EventBind(bindTextBox));
        }
    }
}
